package com.china08.yxyapi.api.schwebsite.album;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.entity.mg.sws.SwsAlbums;
import com.china08.yxyapi.entity.mg.sws.SwsAlbumsImage;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.mg.SwsAlbumsImageRepository;
import com.china08.yxyapi.repository.mg.YxySwsAlbumsRepository;
import com.china08.yxyapi.util.ThumborUtil;

@CrossOrigin
@RestController("SwsAlbumImageListController")
public class SwsAlbumImageListController extends YxyApiBaseController {

    @Autowired
    private SwsAlbumsImageRepository swsAlbumsImageRepository;

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @Autowired
    private YxySwsAlbumsRepository yxySwsAlbumsRepository;

    @RequestMapping(value = "/api_yw/schwebsite/album/images", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String domainName = getDomainName(request);

        String albumId = reqModel.albumId;
        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");

        Assert.isTrue(StringUtils.isNotBlank(albumId), "相册ID为空");
        // 通过域名获取学校ID
        SwsSettingDomains byDomainName = swsSettingDomainsRepository.findByDomainName(domainName);
        Assert.isTrue(null != byDomainName, "域名对象为空");
        String schoolId = byDomainName.getSchoolId();
        List<SwsAlbumsImage> findBySchoolId = swsAlbumsImageRepository.findBySchoolIdAndAlbumId(schoolId, albumId);
        Assert.isTrue(null != byDomainName, "相册列表为空");
        List<RespModel> list = new ArrayList<RespModel>();

        for (SwsAlbumsImage swsAlbumsImage : findBySchoolId) {
            Integer idx = findBySchoolId.indexOf(swsAlbumsImage);
            RespModel model = new RespModel();
            SwsAlbums albums = yxySwsAlbumsRepository.findById(swsAlbumsImage.getAlbumId());
            if (StringUtils.isBlank(albums.getDescription())) {
                model.description = "暂无描述";
            } else {
                model.description = albums.getDescription();
            }
            model.title = albums.getName();
            model.id = swsAlbumsImage.getId();
            model.albumId = swsAlbumsImage.getAlbumId();
            if (StringUtils.isNotBlank(swsAlbumsImage.getImagePath())) {
                if (swsAlbumsImage.getImagePath().startsWith("https")) {
                    model.src = swsAlbumsImage.getImagePath().replace("https:", "");
                    model.src_840_475 = ThumborUtil.schoolWebSiteResize(swsAlbumsImage.getImagePath(), 840, 475);
                    model.src_740_404 = ThumborUtil.schoolWebSiteResize(swsAlbumsImage.getImagePath(), 740, 404);
                    model.smallSrc_110_75 = ThumborUtil.schoolWebSiteResize(swsAlbumsImage.getImagePath(), 110, 75);
                    model.smallSrc_188_152 = ThumborUtil.schoolWebSiteResize(swsAlbumsImage.getImagePath(), 188, 152);
                    model.smallSrc_121_85 = ThumborUtil.schoolWebSiteResize(swsAlbumsImage.getImagePath(), 121, 85);
                    model.smallSrc = swsAlbumsImage.getImagePath().replace("https:", "");
                } else {
                    model.src = swsAlbumsImage.getImagePath().replace("http:", "");
                    model.src_840_475 = ThumborUtil.schoolWebSiteResize(swsAlbumsImage.getImagePath(), 840, 475);
                    model.src_740_404 = ThumborUtil.schoolWebSiteResize(swsAlbumsImage.getImagePath(), 740, 404);
                    model.smallSrc_110_75 = ThumborUtil.schoolWebSiteResize(swsAlbumsImage.getImagePath(), 110, 75);
                    model.smallSrc_188_152 = ThumborUtil.schoolWebSiteResize(swsAlbumsImage.getImagePath(), 188, 152);
                    model.smallSrc_121_85 = ThumborUtil.schoolWebSiteResize(swsAlbumsImage.getImagePath(), 121, 85);
                    model.smallSrc = swsAlbumsImage.getImagePath().replace("http:", "");
                }
            } else {
                model.src = "";
                model.src_840_475 = "";
                model.src_740_404 = "";
                model.smallSrc_110_75 = "";
                model.smallSrc_188_152 = "";
                model.smallSrc_121_85 = "";
                model.smallSrc = "";
            }

            model.schoolId = swsAlbumsImage.getSchoolId();
            model.idx = idx + 1;
            list.add(model);
        }

        return list;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String albumId;

    }

    protected static class RespModel extends YxyApiRespModel {

        public String id;

        public String schoolId;

        public String albumId;

        public String src;

        public String src_840_475;

        public String smallSrc;

        public String smallSrc_110_75;

        public String smallSrc_121_85;

        public String src_740_404;

        public String smallSrc_188_152;

        public String title;

        public String description;

        public Integer totalImg;

        public Integer idx;

    }
}
